//
//  YCProfileViewController.m
//  YCSchool
//
//  Created by yanglinxia on 16/4/27.
//  Copyright © 2016年 yanglinxia. All rights reserved.
//

#import "YCProfileViewController.h"
#import "YCProfileButton.h"
#import "YCDescViewController.h"//简介
#import "YCLifeViewController.h"//生活
#import "YCConsultViewController.h"//咨询

static CGFloat const titleScrollViewH = 36;
static CGFloat const btnWidth = 100;//按钮宽度

@interface YCProfileViewController()<UIScrollViewDelegate>

/** contentScrollView */
@property (nonatomic, weak) UIScrollView *contentScrollView;
/** titleScrollView */
@property (nonatomic, weak) UIScrollView *titleScrollView;
/** 选中的按钮 */
@property (nonatomic, strong) YCProfileButton *currentSelectedBtn;

@end

@implementation YCProfileViewController


- (void)viewDidLoad {
    
    [super viewDidLoad];
    [self setUpNavgationBar];
    [self setupContentScrollView];
    [self setUpChildVcs];
    [self setupTitlesScrollView];
    [self addChildVcViewToScrollView:0];
    [self resloveSilderSlipProblem];
}
- (void)resloveSilderSlipProblem
{
    NSArray *gestureArray = self.navigationController.view.gestureRecognizers;
    for (UIGestureRecognizer *gesture in gestureArray) {
        if ([gesture isKindOfClass:[UIScreenEdgePanGestureRecognizer class]]) {
            [self.contentScrollView.panGestureRecognizer requireGestureRecognizerToFail:gesture];
        }
    }
}
#pragma mark - 初始化
- (void)setUpNavgationBar
{
    self.navigationItem.title = @"一乘驾校";
    [self yc_addBackBarButtonItem];
}

- (void)setupTitlesScrollView
{
    UIScrollView *titlesScrollView = [[UIScrollView alloc] init];
    CGFloat titleScrollViewX = 0;
    CGFloat titleScrollViewY = 0;
    titlesScrollView.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:1];
    titlesScrollView.frame = CGRectMake(titleScrollViewX, titleScrollViewY, self.view.yc_width, titleScrollViewH);
    [self.view addSubview:titlesScrollView];
    _titleScrollView = titlesScrollView;
    [self addTitlesBtn];
}

- (void)setupContentScrollView
{
    UIScrollView *contentScrollView = [[UIScrollView alloc] init];
    contentScrollView.frame = CGRectMake(0, 0, self.view.yc_width, self.view.yc_height-64);
    [self.view addSubview:contentScrollView];
    contentScrollView.delegate = self;
    contentScrollView.pagingEnabled = YES;
    contentScrollView.scrollEnabled = YES;
    contentScrollView.showsHorizontalScrollIndicator = NO;
    self.contentScrollView = contentScrollView;
    
}

- (void)addTitlesBtn
{
    NSUInteger count = self.childViewControllers.count;
    for (NSUInteger i = 0;  i < count; i++) {
        UIViewController *vc = self.childViewControllers[i];
        YCProfileButton *btn = [YCProfileButton buttonWithType:UIButtonTypeCustom];
        [btn setTitle:vc.title forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(titleBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.titleScrollView addSubview:btn];
        CGFloat btnW = count >3 ? btnWidth:(kScreenWidth / 3.0);
        CGFloat btnH = titleScrollViewH;
        CGFloat btnX = i * btnW;
        CGFloat btnY = 0;
        btn.frame = CGRectMake(btnX, btnY, btnW+1, btnH );// 宽度+1，相临button中间为竖线重合变为1，不再是2
        
        CGFloat contentW = count * btnW;
        self.titleScrollView.contentSize = CGSizeMake(contentW, 0);
        self.titleScrollView.showsHorizontalScrollIndicator = NO;
        
        if (i == 0) {
            [self titleBtnClick:btn];
        }
        
    }
    
}

- (void)setUpChildVcs
{
    //简介
    YCDescViewController *descVC = [[YCDescViewController alloc] init];
    descVC.title = @"简介";
    [self addChildViewController:descVC];
    
    //学车生活
    YCLifeViewController *lifeVC = [[YCLifeViewController alloc] init];
    lifeVC.title = @"学车生活";
    [self addChildViewController:lifeVC];
    
    //客服咨询
    YCConsultViewController *consultVC = [[YCConsultViewController alloc] init];
    consultVC.title = @"客服咨询";
    [self addChildViewController:consultVC];
    
    NSInteger count = self.childViewControllers.count;
    _contentScrollView.contentSize = CGSizeMake(count * _contentScrollView.yc_width, 0);
    
}

#pragma mark - 监听

- (void)titleBtnClick:(YCProfileButton *)btn
{
    self.currentSelectedBtn.selected = NO;
    btn.selected = YES;
    self.currentSelectedBtn = btn;
    // 让标题居中
    [self setupTitleCenter:btn];
    // 添加对应控制view到scrollView中
    NSInteger index = [self.titleScrollView.subviews indexOfObject:btn];
    [self addChildVcViewToScrollView:index];
    
}

// 让标题居中
- (void)setupTitleCenter:(UIButton *)btn
{
    CGFloat offsetX = btn.center.x - kScreenWidth * 0.5;
    if (offsetX < 0) {
        offsetX = 0;
    }
    CGFloat maxOffsetX = self.titleScrollView.contentSize.width - kScreenWidth;
    if (offsetX > maxOffsetX) {
        offsetX = maxOffsetX;
    }
    [self.titleScrollView setContentOffset:CGPointMake(offsetX, 0) animated:YES];
    
}

#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    CGFloat offsetX = scrollView.contentOffset.x;
    NSInteger index = offsetX / self.contentScrollView.yc_width;
    [self titleBtnClick:self.titleScrollView.subviews[index]];
}

- (void)addChildVcViewToScrollView:(NSInteger)index
{
    UIViewController *VC = self.childViewControllers[index];
    if (![VC isViewLoaded]) {
        VC.view.frame = CGRectMake(index * self.contentScrollView.yc_width, titleScrollViewH, self.contentScrollView.yc_width, self.contentScrollView.yc_height -  titleScrollViewH);
        [self.contentScrollView addSubview:VC.view];
    }
    [self.contentScrollView setContentOffset:CGPointMake(index * self.contentScrollView.yc_width, 0) animated:NO];
    
}



@end
